package com.example.wangyongdong.bearsqlitedemo.spadd;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;

import com.example.wangyongdong.bearsqlitedemo.HttpManager;
import com.example.wangyongdong.bearsqlitedemo.R;
import com.example.wangyongdong.bearsqlitedemo.db.DatabaseTool;
import com.example.wangyongdong.bearsqlitedemo.entity.BrandEntity;
import com.example.wangyongdong.bearsqlitedemo.entity.CarEntity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by wangyongdong on 2017/4/27.
 */

public class SpAddActivity extends AppCompatActivity {
    private int index, brand, series;
    private Spinner brandsSp, seriesSp, typeSp;
    private SpAddAdapter spAddAdapter;
    private ImageView saveIv;
    private EditText nameEt;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spadd);
        brandsSp = (Spinner) findViewById(R.id.brand_sp);
        seriesSp = (Spinner) findViewById(R.id.series_sp);
        typeSp = (Spinner) findViewById(R.id.type_sp);
        nameEt = (EditText) findViewById(R.id.name_et);
        final String name = nameEt.getText().toString();
        saveIv = (ImageView) findViewById(R.id.sp_save_iv);
        saveIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CarEntity carEntity = new CarEntity();
                carEntity.setName(name);
                DatabaseTool.getInstance().addCar(carEntity);
                finish();
            }
        });

        spAddAdapter = new SpAddAdapter(SpAddActivity.this);

        brands();


        brandsSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, final int position, long id) {
                HttpManager.getInstance().getService()
                        .getBrands()
                        .enqueue(new Callback<BrandEntity>() {
                            @Override
                            public void onResponse(Call<BrandEntity> call, Response<BrandEntity> response) {
                                BrandEntity body = response.body();
                                index = body.getIdxes().get(position);
                                series();
                            }

                            @Override
                            public void onFailure(Call<BrandEntity> call, Throwable t) {

                            }
                        });
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        seriesSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, final int position, long id) {
                Call<BrandEntity> carSeries = HttpManager.getInstance().getService()
                        .getCarSeries(index);
                carSeries.enqueue(new Callback<BrandEntity>() {
                    @Override
                    public void onResponse(Call<BrandEntity> call, Response<BrandEntity> response) {
                        BrandEntity body = response.body();
                        index = body.getIdxes().get(position);
                        brand = body.getPinpai();
                        series = body.getChexi();
//                        type();
                    }

                    @Override
                    public void onFailure(Call<BrandEntity> call, Throwable t) {

                    }
                });
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

//        typeSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//
//            }
//
//            @Override
//            public void onNothingSelected(AdapterView<?> parent) {
//
//            }
//        });

    }



    public void brands() {
        Call<BrandEntity> brands = HttpManager.getInstance().getService()
                .getBrands();
        brands.enqueue(new Callback<BrandEntity>() {
            @Override
            public void onResponse(Call<BrandEntity> call, Response<BrandEntity> response) {
                BrandEntity body = response.body();
                SpAddAdapter spAddAdapter = new SpAddAdapter(SpAddActivity.this);
                spAddAdapter.setBrandEntity(body);
                brandsSp.setAdapter(spAddAdapter);
            }

            @Override
            public void onFailure(Call<BrandEntity> call, Throwable t) {

            }
        });

    }

    public void series() {
        Call<BrandEntity> carSeries = HttpManager.getInstance().getService()
                .getCarSeries(index);
        carSeries.enqueue(new Callback<BrandEntity>() {
            @Override
            public void onResponse(Call<BrandEntity> call, Response<BrandEntity> response) {
                BrandEntity body = response.body();
                SpAddAdapter spAddAdapter = new SpAddAdapter(SpAddActivity.this);
                spAddAdapter.setBrandEntity(body);
                seriesSp.setAdapter(spAddAdapter);
            }

            @Override
            public void onFailure(Call<BrandEntity> call, Throwable t) {

            }
        });

    }

    public void type() {
        Call<BrandEntity> carType = HttpManager.getInstance().getService()
                .getCarType(brand, series);
        carType.enqueue(new Callback<BrandEntity>() {
            @Override
            public void onResponse(Call<BrandEntity> call, Response<BrandEntity> response) {
                BrandEntity body = response.body();
                SpAddAdapter spAddAdapter = new SpAddAdapter(SpAddActivity.this);
                spAddAdapter.setBrandEntity(body);
                typeSp.setAdapter(spAddAdapter);
            }

            @Override
            public void onFailure(Call<BrandEntity> call, Throwable t) {

            }
        });


    }


}
